package com.cn.kabalifeuser.ui.order.activity;

import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.bean.UpImgBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.List;

/**
 * Created by 晓林 on 2020/9/25.
 * function:
 */
public class AfterSalePresenter extends BasePresenter<AfterSaleView> {

    /**
     * 上传文件。
     */
    public void onUpImgData(String img) {
        PostRequest postRequest = OkGo.<UpImgBean>post(Api.API_UP_IMG);
        postRequest.params("type", "head");
        postRequest.params("file[]", new File(img));
        postRequest.execute(new CacheCallBack<UpImgBean>(getView()._getContext(), true) {
            @Override
            public void onSuccess(Response<UpImgBean> response) {
                super.onSuccess(response);
                LoggerUtils.d("上传图:" + response.body());
                if (isViewAttached())
                    if (response.body().getCode() == Constants.HTTP_SUCCESS) {
                        getView().onUpImgSuccess(response.body());
                    } else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN) {
                        getView().onReLoggedIn(response.body().getMsg());
                    } else {
                        getView().onError(response.body().getMsg());
                    }

            }
        });
    }


    public void onAfterSaleData(String token, int id, int after_sale_type, String after_reason, String content, String name, String phone, List<String> img) {
        OkGo.<MsgBean>post(Api.API_ORDER_AFTER_SALE)
                .headers("token", token)
                .params("id", id)
                .params("after_sale_type", after_sale_type)
                .params("after_reason", after_reason)
                .params("name", name)
                .params("phone", phone)
                .params("content", content)
                .addUrlParams("img[]", img)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("评论" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAfterSaleSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
}
